package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.query.DestintionQuery;
import cn.wolfcode.luowowo.search.service.IDestinationSearchService;
import cn.wolfcode.luowowo.search.service.IRouteSearchService;
import cn.wolfcode.luowowo.search.vo.DestFilter;
import cn.wolfcode.luowowo.search.vo.DestVo;
import cn.wolfcode.luowowo.search.vo.ThemeVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IRouteService routeService;
    @Reference
    private IThemeService themeService;
    @Reference
    private IRouteSearchService routeSearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @RequestMapping("")
    //@CheckLoginAnnotation
    public String destinationList(HttpServletRequest req, HttpServletResponse resp, Model model){
        List<Region> hotRegions = regionService.queryHotRegions();
        model.addAttribute("hotRegions",hotRegions);
        Map<String,List<DestinationTemplate>> mouths =routeSearchService.selectBySevenMonth();
        Map<String,List<ThemeVo>> sorts=routeSearchService.selectThemeSixteenForTheme();
        model.addAttribute("mouths",mouths);
        model.addAttribute("sorts",sorts);
        return "destination/index";
    }

    @RequestMapping("getHotDestByRegionId")
    //@CheckLoginAnnotation
    public String destinationList(@ModelAttribute("regionId") Long regionId, Model model){
        List<Destination> list = destinationService.selectDestByRegionId(regionId);

        //左边
        List<Destination> left = list.subList(0, list.size() / 2 + 1);
        model.addAttribute("leftDests",left);

        //右边

        List<Destination> right = list.subList(list.size() / 2 + 1,list.size());
        model.addAttribute("rightDests",right);
        return "destination/hotdestTpl";
    }


    @RequestMapping("guide")
    public String guide(Long id, Model model){
        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        //System.err.println(dest);//广州
        model.addAttribute("toasts",toasts);
        //dest
        //Destination dest = destinationService.getDestById(id);
        model.addAttribute("dest",dest);
        //catalogs
        model.addAttribute("catalogs",strategyCatalogService.queryCatalogByDestId(id));
        //strategyDetails
        List<StrategyDetail> details = strategyDetailService.queryViewNumTop3(id);
        model.addAttribute("strategyDetails",details);
        return "destination/guide";
    }

    @RequestMapping("surveyPage")
    public String surveyPage(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "destination/survey";
    }


    @RequestMapping("survey")
    public String survey(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model){

        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        //catalog
        StrategyCatalog catalog1 = null;
        for (StrategyCatalog catalog : catalogs) {
            if (catalog.getId()==qo.getCatalogId()){
                catalog1 = catalog;
                model.addAttribute("catalog",catalog);
            }
        }
        //detail
        if(catalog1 != null){
            List<StrategyDetail> details = catalog1.getDetails();
            if(details != null && details.size() > 0) {
                StrategyDetail detail = details.get(0);
                detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                model.addAttribute("detail", details.get(0));
            }
        }
        return "destination/surveyTpl";
    }

    @RequestMapping("travels")
    public String list(@ModelAttribute("qo") TravelQuery qo, Model model){
        //pageInfo
        //多条件查询
        System.err.println(qo.getDayType());
        PageInfo<Travel> pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }
    @RequestMapping("destFilter")
    public String destFilter(Model model,Long id,Long tid){
        PageInfo<Object> pageInfo = new PageInfo<>();
        model.addAttribute("pageInfo",pageInfo);
        Map<Long, List<ThemeVo>> stringListMap = routeSearchService.selectAllTheme();
        model.addAttribute("allRight",stringListMap.get(Theme.ALL_RIGHT_SORT));
        model.addAttribute("season",stringListMap.get(Theme.SEASON_SORT));
        model.addAttribute("trip",stringListMap.get(Theme.TRIP_SORT));
        model.addAttribute("holiday",stringListMap.get(Theme.HOLIDAY_SORT));
        model.addAttribute("themeId",id);
        model.addAttribute("timeId",tid);
        return "destination/destFilter";
    }
    @RequestMapping("condition")
    public String condition(Model model,@ModelAttribute("qo") DestintionQuery qo){
        List<DestVo> destVos = destinationSearchService.selectDestBYCondition(qo);
        model.addAttribute("destVos",destVos);
        return "destination/destFilterTpl";
    }
    @RequestMapping("map")
    public String map(){
        return "map";
    }
}
